
function setData(bool, msg) {
  data.bool = bool
  data.msg = msg
  return data
}

function checkFn(regx, name, empty, prompt) {
  if (!name) return setData(false, empty)
  if (!regx.test(name)) {
    return setData(false, prompt)
  }
  return data
}

function Params(datas) {
  let self = this
  self.datas = datas
}

Params.prototype = {
  name: (name) => {
    let regx = /^[A-Za-z0-9\u4e00-\u9fa5]{2,20}$/
    return checkFn(regx, name, '姓名不能为空!', '请输入长度为2-20位的有效名字!')
  },
  channel_type: (channel_type) => {
    let regx = /^[A-Za-z0-9\u4e00-\u9fa5]{2,20}$/
    return checkFn(regx, channel_type, '姓名不能为空!', '请输入长度为2-20位的有效名字!')
  },
  account: (account) => {
    if (!account) return setData(false, '请正确输入支付宝帐号!')
    return data
  },
  channel_type: (channel_type) => {
    if (!channel_type) return setData(false, '请正确输入支付宝帐号!')
    return data;
  },
  price: (price) => {
    if (!price) return setData(false, '请正确输入提现金额!')
    return data;
  },
  channel_type: (channel_type) => {
    let regx = /^[A-Za-z0-9\u4e00-\u9fa5]{2,20}$/
    return checkFn(regx, channel_type, '姓名不能为空!', '请输入长度为2-20位的有效名字!')
  },
  phone: (phone) => {
    let regx = /^1[0-9]{10}$/
    return checkFn(regx, phone, '手机号码不能为空!', '请正确输入手机号码!')
  },
  valid_no: (valid_no) => {
    let regx = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    return checkFn(regx, valid_no, '身份证不能为空!', '请正确输入身份证!')
  },
  city: (city) => {
    let regx = /^[A-Za-z0-9\u4e00-\u9fa5]{2,300}$/
    return checkFn(regx, city, '城市不能为空!', '请输入城市!')
  },
  sex: (sex) => {
    if (!sex) return setData(false, '请输入所属机构名称!')
    return data
  },
  account_name: (account_name) => {
    if (!account_name) return setData(false, '请输入所属机构名称!')
    return data
  },
  title: (title) => {
    if (!title) return setData(false, '请选择导师职称!')
    return data
  },
  pictures: (pictures) => {
    let images = ['/images/m/zhengmian.png', '/images/m/fanmian.png', '/images/m/zhengshu.png'];
    if (!!!pictures[0] || pictures[0] == images[0]) {
      return setData(false, '请上传身份证正面照!')
    } else if (!!!pictures[1] || pictures[1] == images[1]) {
      return setData(false, '请上传身份证反面照!')
    } else if (!!!pictures[2] || pictures[2] == images[2]) {
      return setData(false, '请上传资质证书照片!')
    } else {
      return data
    }
  }
}

let data
let params
function checkParams(app, datas, title) {
  params = new Params(datas)
  for (let v in datas) {
    data = { bool: true, msg: '' }
    let fn = params[v]
    let value = datas[v]
    data = fn(value)
    if (!data.bool) {
      app.showModel({ title: title || '', msg: data.msg })
      return data.msg
      // temps.push(data.msg)
    }
  }
  // return temps
}

function formatParams(...args) {
  let data = args.shift()
  let params = {}
  for (let v in data) {
    args.map(function (a, b) {
      if (v == a) {
        params[v] = data[v]
      }
    })
  }

  return params;
}

module.exports = {
  formatParams: formatParams,
  checkParams: checkParams
}